package org.senseframework.support.orm.hbase.definition;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.CollectionUtils;
import org.senseframework.support.orm.hbase.annotation.ColumnType;
import org.senseframework.support.orm.hbase.serializer.ISerialize;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * @author wangqian
 * @date 18/4/12 下午8:22
 */
public class HBaseEntityDefinition {

    public static final Logger logger = LoggerFactory.getLogger(HBaseEntityDefinition.class);
    private String table;
    private String idJavaColumn;
    private Class idType;
    private Map<String, HBaseColumnDefinition> columnDefinitions = new HashMap<String, HBaseColumnDefinition>();
    private ISerialize serialize;

    public ISerialize getSerialize() {
        return serialize;
    }

    public void setSerialize(ISerialize serialize) {
        this.serialize = serialize;
    }

    public String getTable() {
        return table;
    }

    public void setTable(String table) {
        this.table = table;
    }

    public byte[] tableBytes() {
        if (table == null) {
            return null;
        }
        return Bytes.toBytes(table);
    }

    public String getIdJavaColumn() {
        return idJavaColumn;
    }

    public void setIdJavaColumn(String idJavaColumn) {
        this.idJavaColumn = idJavaColumn;
    }

    public Class getIdType() {
        return idType;
    }

    public void setIdType(Class idType) {
        this.idType = idType;
    }

    public Map<String, HBaseColumnDefinition> getColumnDefinitions() {
        return columnDefinitions;
    }

    public void setColumnDefinitions(Map<String, HBaseColumnDefinition> columnDefinitions) {
        this.columnDefinitions = columnDefinitions;
    }

    public void addColumnDefinition(String javaColumn, HBaseColumnDefinition columnDefinition) {
        columnDefinitions.put(javaColumn, columnDefinition);
    }

    public <T> T resultToEntity(Result result, Class<T> clazz) throws Exception {
        if (result.isEmpty()) {
            return null;
        }
        T obj = clazz.newInstance();
        if (MapUtils.isNotEmpty(columnDefinitions)) {
            for (HBaseColumnDefinition columnDefinition : columnDefinitions.values()) {
                try {
                    if (ColumnType.QUALIFIER.equals(columnDefinition.getColumnType())) {
                        if (!CollectionUtils.isEmpty(result.listCells())) {
                            Map value = new TreeMap();
                            for (Cell cell : result.listCells()) {
                                value.put(serialize.decode(cell.getQualifier(), columnDefinition.getKeyType()),
                                        serialize.decode(cell.getValue(), columnDefinition.getValueType()));
                            }
                            PropertyUtils.setProperty(obj, columnDefinition.getJavaColumn(), value);
                        }
                    } else {
                        byte[] bytes = result.getValue(Bytes.toBytes(columnDefinition.getFamily()), Bytes.toBytes(columnDefinition.getQualifier()));
                        Object value = serialize.decode(bytes, columnDefinition.getType());
                        if (value != null) {
                            PropertyUtils.setProperty(obj, columnDefinition.getJavaColumn(), value);
                        }
                    }
                } catch (Exception e) {
                    logger.error("resultToEntity", e);
                }
            }
        }
        if (StringUtils.isNotBlank(getIdJavaColumn())) {
            PropertyUtils.setProperty(obj, getIdJavaColumn(), serialize.decode(result.getRow(), getIdType()));
        }
        return obj;
    }

    public <T> List<T> resultToEntity(Result[] results, Class<T> clazz) throws Exception {
        List<T> list = new ArrayList<T>();
        for (Result result : results) {
            T obj = resultToEntity(result, clazz);
            if (obj != null) {
                list.add(obj);
            }
        }
        return list;
    }
}
